package com.chhuang.common;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ThreadPoolShutdownUtil {
	private static final Log LOG = LogFactory.getLog(ThreadPoolShutdownUtil.class);
	
	/**
	 * 设置超时关闭线程池
	 * 
	 * @param pool
	 */
	public static void shutdownPool(ExecutorService pool, long timeout, TimeUnit unit){
		pool.shutdown();
		try {
			if(pool!=null && !pool.awaitTermination(timeout, unit)){
				LOG.error("规定时间内未完成所有任务,取消所有遗留的任务");
				pool.shutdownNow();
				pool = null;
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
